package com.beagle.njddapp.base;

import android.text.TextUtils;

import com.beagle.njddapp.core.UserService;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * @author hxx
 * @date 2021-03-17 13:56
 */
public class HeaderInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {



        //请求token
//        String token = SPUtils.getInstance(SPConstant.TOKEN).getString(SPConstant.TOKEN);

        Request request = chain.request();
        Request updateRequest;
        if (request.url().host().contains("alicloudapi")||request.url().host().contains("aliyun")){
            updateRequest= request.newBuilder().header("Authorization","APPCODE 62f7a4e4f74d4b28b0096a7e8fe51e8b")
                    .header("Content-Type","application/x-www-form-urlencoded; charset=UTF-8")
                    .build();
            return chain.proceed(updateRequest);
        }else {
            if (TextUtils.isEmpty(UserService.getInstance().getToken())){
                updateRequest= request.newBuilder().header("Content-Type","application/json;charset=UTF-8")
                        .build();
            }else {
                updateRequest=request.newBuilder()
                        .header("Content-Type","application/json;charset=UTF-8")
                        .header("Authorization","Bearer " +UserService.getInstance().getToken())
                        .build();
            }
            return chain.proceed(updateRequest);
        }



    }
}
